﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerUI.UIControl
{
    public class FloatToolStrip : ToolStrip
    {
        private ToolStripPanel tsPanel;

        public FloatToolStrip()
        {
            this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
            this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
        }

        private ToolStripFloatWindow floatForm;
        public ToolStripFloatWindow FloatForm
        {
            get { return floatForm; }
            set
            {
                floatForm = value;
                if (floatForm != null)
                {
                    floatForm.LocationChanged += new EventHandler(floatForm_LocationChanged);
                    floatForm.FormClosing += new FormClosingEventHandler(floatForm_FormClosing);
                }
            }
        }

        void floatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void floatForm_LocationChanged(object sender, EventArgs e)
        {
            //当floatwindws的位置移动到toolstrippanel中时，将this放置到 toolstripPanel上 
            if (this.floatForm == null)
            {
                return;
            }
            else
            {
                if (floatForm.HasCreated)
                {
                    this.floatForm.ClientSize = this.Size;
                    Point currentPt = new Point(floatForm.Location.X, floatForm.Location.Y);
                    Point minPt = this.tsPanel.PointToScreen(tsPanel.Location);
                    Point maxPt;
                    if (this.tsPanel.Height <= 20)
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + 20);

                    }
                    else
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + this.tsPanel.Height);
                    }

                    if ((currentPt.X > minPt.X) && (currentPt.X < maxPt.X) && (currentPt.Y > minPt.Y - 25) && (currentPt.Y < maxPt.Y - 25))
                    {
                        this.floatForm.Controls.Remove(this);
                        this.tsPanel.SuspendLayout();
                        this.tsPanel.Controls.Add(this);
                        this.Location = this.tsPanel.PointToClient(currentPt);
                        this.tsPanel.ResumeLayout();
                        this.floatForm.Dispose();
                        this.floatForm = null;
                    }
                }
            }
        }

        public bool isFloating
        {
            get
            {
                return (floatForm != null);
            }
        }

        public ToolStripPanel ToolStripPanel
        {
            get
            {
                return this.tsPanel;
            }
            set
            {
                this.tsPanel = value;
            }
        }

        private void MyToolStrip_EndDrag(object sender, EventArgs e)
        {
            //判断移除时
            if (this.tsPanel == null)
            {
                MessageBox.Show("请先设置ToolStripPanel属性");
                return;
            }
            Point dockPoint = Cursor.Position;
            int openX, openY;
            openX = dockPoint.X;
            openY = dockPoint.Y;

            Point clientPt = this.tsPanel.Parent.PointToClient(dockPoint);
            if (clientPt.Y > tsPanel.Height)
            {
                ToolStripFloatWindow tsfw = new ToolStripFloatWindow();
                this.tsPanel.Controls.Remove(this);
                tsfw.Controls.Add(this);
                this.Left = 0;
                this.Top = 0;
                this.FloatForm = tsfw;
                Point newLoc = new Point(openX, openY);
                tsfw.Show();
                tsfw.Location = newLoc;
            }
        }

        private void MyToolStrip_SizeChanged(object sender, EventArgs e)
        {
            if (this.isFloating)
            {
                this.floatForm.ClientSize = this.Size;
            }
        }
    }

    public class ToolStripFloatWindow : Form
    {
        //解决鼠标拖动后，已经正在创建，但是触发Dispose的问题
        private bool hasCreated = false;
        public bool HasCreated
        {
            get
            {
                return hasCreated;
            }
        }

        public ToolStripFloatWindow()
            : base()
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "ToolStripFloatWindow";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.Load += new System.EventHandler(this.ToolStripFloatWindow_Load);
        }

        private void ToolStripFloatWindow_Load(object sender, EventArgs e)
        {
            this.hasCreated = true;
        }
    }

}
